package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Cart;
import csu.web.mypetstore.domain.Item;
import csu.web.mypetstore.domain.Log;
import csu.web.mypetstore.persistence.CartDao;
import csu.web.mypetstore.persistence.impl.CartDaoImpl;
import csu.web.mypetstore.service.CatalogService;
import csu.web.mypetstore.service.LogService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;


import java.io.IOException;
import java.util.List;

public class AddItemToCartServlet extends HttpServlet {

    private static final String CART_FORM = "/WEB-INF/jsp/cart//cart.jsp";

//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String workingItemId = req.getParameter("workingItemId");
//
//        HttpSession session = req.getSession();
//        Cart cart = (Cart) session.getAttribute("cart");
//
//        if (cart == null) {
//            cart = new Cart();
//        }
//
//        if(cart.containsItemId(workingItemId)){
//            cart.incrementQuantityByItemId(workingItemId);
//        }
//        else{
////            CatalogService catalogService = new CatalogService();
////            boolean isInStock = CatalogService.isItemInStock(workingItemId);
////            Item item = CatalogService.getItem(workingItemId);
////            cart.addItem(item,isInStock);
//        }
//
//        session.setAttribute("cart", cart);
//        session.removeAttribute("workingItemId");
//        req.getRequestDispatcher(CART_FORM).forward(req, resp);
//    }

//    private static final String VIEW_CART = "/WEB-INF/jsp/cart/Cart.jsp";
    private String workingItemId;
    private CatalogService catalogService;
    private Cart cart;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        workingItemId = req.getParameter("workingItemId");

        HttpSession httpSession = req.getSession();
        cart = (Cart) httpSession.getAttribute("cart");

        if (cart == null) {
            cart = new Cart();
        }
        if (workingItemId!=null) {
            if (cart.containsItemId(workingItemId)) {
                cart.incrementQuantityByItemId(workingItemId);
                Account account = (Account) httpSession.getAttribute("account");
                if (account != null) {
                    CartDao cartDAO = new CartDaoImpl();
                    cartDAO.updateCartInf(cart.getCartItemList(), account);
                    LogService logService = new LogService();
                    List<Log> logList = (List<Log>) httpSession.getAttribute("logList");
                    logService.updateLog(logList, "Add " + workingItemId + " to cart.", account);
                    httpSession.setAttribute("logList", logList);
                }
            } else {
                catalogService = new CatalogService();
                boolean isInStock = catalogService.isItemInStock(workingItemId);
                Item item = catalogService.getItem(workingItemId);
                cart.addItem(item, isInStock);
                Account account = (Account) httpSession.getAttribute("account");
//                if (account != null) {
//                    CartDao cartDAO = new CartDaoImpl();
//                    cartDAO.updateCartInf(cart.getCartItemList(), account);
//                    LogService logService = new LogService();
//                    List<Log> logList = (List<Log>) httpSession.getAttribute("logList");
//                    logService.updateLog(logList, "Add " + workingItemId + " to cart.", account);
//                    httpSession.setAttribute("logList", logList);
//                }
            }
        }
        cart.setTotal(cart.getSubTotal());
        httpSession.setAttribute("cart", cart);
        httpSession.removeAttribute("workingItemId");
        req.getRequestDispatcher(CART_FORM).forward(req, resp);
    }

}
